package com.salary.model;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Chinese implements Comparable<Chinese> {
	private int id;
	private String chinese;

	@Override
	public int compareTo(Chinese o) {
		Pattern p = Pattern.compile("\t|\r|\n");
		Matcher m1 = p.matcher(this.chinese);
		Matcher m2 = p.matcher(o.chinese);
		String st1 = m1.replaceAll("");
		st1 = st1.replaceAll(" ", "");
		String st2 = m2.replaceAll("");
		st2 = st2.replaceAll(" ", "");
		byte[] bs1 = st1.getBytes();
		byte[] bs2 = st2.getBytes();
		// 用新的字符编码生成字符串
		String ss1 = "";
		String ss2 = "";
		try {
			ss1 = new String(bs1, "UTF-8");
			ss2 = new String(bs2, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Character> list1 = new ArrayList<Character>();
		List<Character> list2 = new ArrayList<Character>();
		char[] ch = ss1.toCharArray();
		for (int j = 0; j < ch.length; j++) {
			char c = ch[j];
			list1.add(new Character(c));
			Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		}
		char[] ch1 = ss2.toCharArray();
		for (int j = 0; j < ch1.length; j++) {
			char c = ch1[j];
			list2.add(new Character(c));
			Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		}

		for (Iterator<Character> iter = list1.iterator(); iter.hasNext();) {
			Character ch2 = iter.next();
			if (!isChinese(ch2.charValue())) {
				iter.remove();
			}
		}
		for (Iterator<Character> iter = list2.iterator(); iter.hasNext();) {
			Character ch2 = iter.next();
			if (!isChinese(ch2.charValue())) {
				iter.remove();
			}
		}

		Character[] ch11 = list1.toArray(new Character[list1.size()]);
		Character[] ch12 = list2.toArray(new Character[list2.size()]);
		char[] ch13 = new char[ch11.length];
		char[] ch14 = new char[ch12.length];
		for (int k = 0; k < ch11.length; k++) {
			ch13[k] = ch11[k].charValue();
		}
		for (int k = 0; k < ch12.length; k++) {
			ch14[k] = ch12[k].charValue();
		}
		String sss1 = new String(ch13);
		String sss2 = new String(ch14);
		if (sss1.equals(sss2)) {
			return 1;
		} else {
			return 0;
		}

	}

	// 根据Unicode编码完美的判断中文汉字和符号
	private boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		}
		return false;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getChinese() {
		return chinese;
	}

	public void setChinese(String chinese) {
		this.chinese = chinese;
	}

}
